package cn.wilson.hrm.web.controller;

import cn.wilson.hrm.utils.AjaxResult;
import cn.wilson.hrm.utils.RedisUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


//操作redis的

@RestController
public class RedisController {

    /**
     *  设置值
     * @param key
     * @param value
     * @return
     */
    //@RequestParam("key") ：从请求中获取名字为key的数据
    @RequestMapping(value = "/redis/set",method = RequestMethod.POST)
    public AjaxResult set(@RequestParam("key") String key,@RequestParam("value")String value){
        RedisUtils.INSTANCE.set(key,value);
        return AjaxResult.me();
    }


    /**
     *  设置带有过期时间的值
     * @param key
     * @param value
     * @return
     */
    //@RequestParam("key") ：从请求中获取名字为key的数据
    @RequestMapping(value = "/redis/setex",method = RequestMethod.POST)
    public AjaxResult setex(@RequestParam("key") String key,@RequestParam("seconds") int seconds,@RequestParam("value")String value){
        RedisUtils.INSTANCE.set(key,seconds,value);
        return AjaxResult.me();
    }

    /**
     *      获取值
     * @param key
     * @return
     */
    @RequestMapping(value = "/redis/get/{key}",method = RequestMethod.GET)
    public AjaxResult get(@PathVariable("key") String key){
        String result = RedisUtils.INSTANCE.get(key);
        return AjaxResult.me().setResultObj(result);
    }


    /**
     *      清空缓存
     * @param key
     * @return
     */
    @RequestMapping(value = "/redis/reset/{key}",method = RequestMethod.POST)
    public AjaxResult reSet(@PathVariable("key") String key){
        RedisUtils.INSTANCE.reSet(key);
        return AjaxResult.me();
    }



}
